package cn.edu.scau.cmi.hombio.web.rest;

import cn.edu.scau.cmi.hombio.dao.EmployeeDAO;
import cn.edu.scau.cmi.hombio.dao.ExceptDAO;
import cn.edu.scau.cmi.hombio.dao.ExceptionTypeDAO;
import cn.edu.scau.cmi.hombio.dao.TaskDAO;

import cn.edu.scau.cmi.hombio.domain.Employee;
import cn.edu.scau.cmi.hombio.domain.Except;
import cn.edu.scau.cmi.hombio.domain.ExceptionType;
import cn.edu.scau.cmi.hombio.domain.Task;

import cn.edu.scau.cmi.hombio.service.ExceptService;

import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;

import org.springframework.stereotype.Controller;

import org.springframework.web.bind.WebDataBinder;

import org.springframework.web.bind.annotation.InitBinder;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

/**
 * Spring Rest controller that handles CRUD requests for Except entities
 * 
 */

@Controller("ExceptRestController")

public class ExceptRestController {

	/**
	 * DAO injected by Spring that manages Employee entities
	 * 
	 */
	@Autowired
	private EmployeeDAO employeeDAO;

	/**
	 * DAO injected by Spring that manages Except entities
	 * 
	 */
	@Autowired
	private ExceptDAO exceptDAO;

	/**
	 * DAO injected by Spring that manages ExceptionType entities
	 * 
	 */
	@Autowired
	private ExceptionTypeDAO exceptionTypeDAO;

	/**
	 * DAO injected by Spring that manages Task entities
	 * 
	 */
	@Autowired
	private TaskDAO taskDAO;

	/**
	 * Service injected by Spring that provides CRUD operations for Except entities
	 * 
	 */
	@Autowired
	private ExceptService exceptService;

	/**
	 * Get Task entity by Except
	 * 
	 */
	@RequestMapping(value = "/Except/{except_id}/task", method = RequestMethod.GET)
	@ResponseBody
	public Task getExceptTask(@PathVariable Integer except_id) {
		return exceptDAO.findExceptByPrimaryKey(except_id).getTask();
	}

	/**
	* Get ExceptionType entity by Except
	* 
	*/
	@RequestMapping(value = "/Except/{except_id}/exceptionType", method = RequestMethod.GET)
	@ResponseBody
	public ExceptionType getExceptExceptionType(@PathVariable Integer except_id) {
		return exceptDAO.findExceptByPrimaryKey(except_id).getExceptionType();
	}

	/**
	* Save an existing Employee entity
	* 
	*/
	@RequestMapping(value = "/Except/{except_id}/employee", method = RequestMethod.PUT)
	@ResponseBody
	public Employee saveExceptEmployee(@PathVariable Integer except_id, @RequestBody Employee employee) {
		exceptService.saveExceptEmployee(except_id, employee);
		return employeeDAO.findEmployeeByPrimaryKey(employee.getId());
	}

	/**
	* Show all Except entities
	* 
	*/
	@RequestMapping(value = "/Except", method = RequestMethod.GET)
	@ResponseBody
	public List<Except> listExcepts() {
		return new java.util.ArrayList<Except>(exceptService.loadExcepts());
	}

	/**
	* Save an existing ExceptionType entity
	* 
	*/
	@RequestMapping(value = "/Except/{except_id}/exceptionType", method = RequestMethod.PUT)
	@ResponseBody
	public ExceptionType saveExceptExceptionType(@PathVariable Integer except_id, @RequestBody ExceptionType exceptiontype) {
		exceptService.saveExceptExceptionType(except_id, exceptiontype);
		return exceptionTypeDAO.findExceptionTypeByPrimaryKey(exceptiontype.getId());
	}

	/**
	* View an existing Employee entity
	* 
	*/
	@RequestMapping(value = "/Except/{except_id}/employee/{employee_id}", method = RequestMethod.GET)
	@ResponseBody
	public Employee loadExceptEmployee(@PathVariable Integer except_id, @PathVariable Integer related_employee_id) {
		Employee employee = employeeDAO.findEmployeeByPrimaryKey(related_employee_id, -1, -1);

		return employee;
	}

	/**
	* Delete an existing Employee entity
	* 
	*/
	@RequestMapping(value = "/Except/{except_id}/employee/{employee_id}", method = RequestMethod.DELETE)
	@ResponseBody
	public void deleteExceptEmployee(@PathVariable Integer except_id, @PathVariable Integer related_employee_id) {
		exceptService.deleteExceptEmployee(except_id, related_employee_id);
	}

	/**
	* View an existing ExceptionType entity
	* 
	*/
	@RequestMapping(value = "/Except/{except_id}/exceptionType/{exceptiontype_id}", method = RequestMethod.GET)
	@ResponseBody
	public ExceptionType loadExceptExceptionType(@PathVariable Integer except_id, @PathVariable Integer related_exceptiontype_id) {
		ExceptionType exceptiontype = exceptionTypeDAO.findExceptionTypeByPrimaryKey(related_exceptiontype_id, -1, -1);

		return exceptiontype;
	}

	/**
	* Create a new Except entity
	* 
	*/
	@RequestMapping(value = "/Except", method = RequestMethod.POST)
	@ResponseBody
	public Except newExcept(@RequestBody Except except) {
		exceptService.saveExcept(except);
		return exceptDAO.findExceptByPrimaryKey(except.getId());
	}

	/**
	* Delete an existing Except entity
	* 
	*/
	@RequestMapping(value = "/Except/{except_id}", method = RequestMethod.DELETE)
	@ResponseBody
	public void deleteExcept(@PathVariable Integer except_id) {
		Except except = exceptDAO.findExceptByPrimaryKey(except_id);
		exceptService.deleteExcept(except);
	}

	/**
	* Delete an existing Task entity
	* 
	*/
	@RequestMapping(value = "/Except/{except_id}/task/{task_id}", method = RequestMethod.DELETE)
	@ResponseBody
	public void deleteExceptTask(@PathVariable Integer except_id, @PathVariable Integer related_task_id) {
		exceptService.deleteExceptTask(except_id, related_task_id);
	}

	/**
	* Create a new Task entity
	* 
	*/
	@RequestMapping(value = "/Except/{except_id}/task", method = RequestMethod.POST)
	@ResponseBody
	public Task newExceptTask(@PathVariable Integer except_id, @RequestBody Task task) {
		exceptService.saveExceptTask(except_id, task);
		return taskDAO.findTaskByPrimaryKey(task.getId());
	}

	/**
	* Select an existing Except entity
	* 
	*/
	@RequestMapping(value = "/Except/{except_id}", method = RequestMethod.GET)
	@ResponseBody
	public Except loadExcept(@PathVariable Integer except_id) {
		return exceptDAO.findExceptByPrimaryKey(except_id);
	}

	/**
	* View an existing Task entity
	* 
	*/
	@RequestMapping(value = "/Except/{except_id}/task/{task_id}", method = RequestMethod.GET)
	@ResponseBody
	public Task loadExceptTask(@PathVariable Integer except_id, @PathVariable Integer related_task_id) {
		Task task = taskDAO.findTaskByPrimaryKey(related_task_id, -1, -1);

		return task;
	}

	/**
	* Delete an existing ExceptionType entity
	* 
	*/
	@RequestMapping(value = "/Except/{except_id}/exceptionType/{exceptiontype_id}", method = RequestMethod.DELETE)
	@ResponseBody
	public void deleteExceptExceptionType(@PathVariable Integer except_id, @PathVariable Integer related_exceptiontype_id) {
		exceptService.deleteExceptExceptionType(except_id, related_exceptiontype_id);
	}

	/**
	* Save an existing Except entity
	* 
	*/
	@RequestMapping(value = "/Except", method = RequestMethod.PUT)
	@ResponseBody
	public Except saveExcept(@RequestBody Except except) {
		exceptService.saveExcept(except);
		return exceptDAO.findExceptByPrimaryKey(except.getId());
	}

	/**
	* Get Employee entity by Except
	* 
	*/
	@RequestMapping(value = "/Except/{except_id}/employee", method = RequestMethod.GET)
	@ResponseBody
	public Employee getExceptEmployee(@PathVariable Integer except_id) {
		return exceptDAO.findExceptByPrimaryKey(except_id).getEmployee();
	}

	/**
	* Register custom, context-specific property editors
	* 
	*/
	@InitBinder
	public void initBinder(WebDataBinder binder, HttpServletRequest request) { // Register static property editors.
		binder.registerCustomEditor(java.util.Calendar.class, new org.skyway.spring.util.databinding.CustomCalendarEditor());
		binder.registerCustomEditor(byte[].class, new org.springframework.web.multipart.support.ByteArrayMultipartFileEditor());
		binder.registerCustomEditor(boolean.class, new org.skyway.spring.util.databinding.EnhancedBooleanEditor(false));
		binder.registerCustomEditor(Boolean.class, new org.skyway.spring.util.databinding.EnhancedBooleanEditor(true));
		binder.registerCustomEditor(java.math.BigDecimal.class, new org.skyway.spring.util.databinding.NaNHandlingNumberEditor(java.math.BigDecimal.class, true));
		binder.registerCustomEditor(Integer.class, new org.skyway.spring.util.databinding.NaNHandlingNumberEditor(Integer.class, true));
		binder.registerCustomEditor(java.util.Date.class, new org.skyway.spring.util.databinding.CustomDateEditor());
		binder.registerCustomEditor(String.class, new org.skyway.spring.util.databinding.StringEditor());
		binder.registerCustomEditor(Long.class, new org.skyway.spring.util.databinding.NaNHandlingNumberEditor(Long.class, true));
		binder.registerCustomEditor(Double.class, new org.skyway.spring.util.databinding.NaNHandlingNumberEditor(Double.class, true));
	}

	/**
	* Create a new Employee entity
	* 
	*/
	@RequestMapping(value = "/Except/{except_id}/employee", method = RequestMethod.POST)
	@ResponseBody
	public Employee newExceptEmployee(@PathVariable Integer except_id, @RequestBody Employee employee) {
		exceptService.saveExceptEmployee(except_id, employee);
		return employeeDAO.findEmployeeByPrimaryKey(employee.getId());
	}

	/**
	* Create a new ExceptionType entity
	* 
	*/
	@RequestMapping(value = "/Except/{except_id}/exceptionType", method = RequestMethod.POST)
	@ResponseBody
	public ExceptionType newExceptExceptionType(@PathVariable Integer except_id, @RequestBody ExceptionType exceptiontype) {
		exceptService.saveExceptExceptionType(except_id, exceptiontype);
		return exceptionTypeDAO.findExceptionTypeByPrimaryKey(exceptiontype.getId());
	}

	/**
	* Save an existing Task entity
	* 
	*/
	@RequestMapping(value = "/Except/{except_id}/task", method = RequestMethod.PUT)
	@ResponseBody
	public Task saveExceptTask(@PathVariable Integer except_id, @RequestBody Task task) {
		exceptService.saveExceptTask(except_id, task);
		return taskDAO.findTaskByPrimaryKey(task.getId());
	}
}